package kr.hkit.androidcomplete02.ch19;

import kr.hkit.androidcomplete02.R;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

public class LongTimeEx3 extends Activity {
	int mValue;
	TextView tv;
	ProgressDialog mPro;
	boolean mQuit;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch19_longtime);
		
		tv = (TextView) findViewById(R.id.ch19_lomgtime_tv);
	}
	
	public void mOnClick(View v){
		mValue = 0;
		showDialog(0);
		mQuit = false;
		handler.sendEmptyMessage(0);
	}

	@SuppressWarnings("deprecation")
	protected android.app.Dialog onCreateDialog(int id) {
		switch(id){
		case 0:
			mPro = new ProgressDialog(this);
			mPro.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
			mPro.setMessage("Updating");
			mPro.setCancelable(true);
			mPro.setButton("Cancle", new DialogInterface.OnClickListener() {
				
				@SuppressWarnings("deprecation")
				@Override
				public void onClick(DialogInterface dialog, int which) {
					mQuit = true;
					dismissDialog(0);
				}
			}); 
			return mPro;
		}
		
		return null;
	};
	
	Handler handler = new Handler(){
		public void handleMessage(Message msg){
			mValue++;
			tv.setText(Integer.toString(mValue));
		
			try{Thread.sleep(50);}catch(InterruptedException e){}
			
			if(mValue <100 && mQuit ==false){
				mPro.setProgress(mValue);
				handler.sendEmptyMessage(0);
			}else{
				dismissDialog(0);
			}
		}
	};
}
